using System;
using System.Xml;
using Xunit.Sdk;
using Xunit;

namespace SubSpec
{
    internal class ActionTestCommand : ITestCommand
    {
        private readonly Action _action;
        private readonly IMethodInfo _method;

        public ActionTestCommand( IMethodInfo method, string name, int timeout, Action action )
        {
            _method = method;
            _action = action;

            Name = name;
            Timeout = timeout;
        }

        public string Name { get; private set; }

        public string DisplayName
        {
            get { return Name; }
        }

        public bool ShouldCreateInstance
        {
            get { return false; }
        }

        public int Timeout { get; private set; }

        public MethodResult Execute( object testClass )
        {
            _action();

            return new PassedResult( _method, Name );
        }

        public virtual XmlNode ToStartXml()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml( "<dummy/>" );
            XmlNode testNode = XmlUtility.AddElement( doc.ChildNodes[0], "start" );

            XmlUtility.AddAttribute( testNode, "name", DisplayName );
            XmlUtility.AddAttribute( testNode, "type", _method.TypeName );
            XmlUtility.AddAttribute( testNode, "method", _method.Name );

            return testNode;
        }
    }
}
